<?php

class ForgetPassword extends MY_Controller {

    public function __construct() {
        parent::__construct();
    }

    function index() {
        $this->initForm();
//        $memberId = $this->session->userdata('memberID');
//        $this->load->model("Member_Model");
//        $this->data['data_member'] = $this->Member_Model->getMemberByID($memberId);
        $this->data['alert_msg'] = "";
        $this->data['main_content'] = 'forget_password';
        $this->load->view('includes/template', $this->data);
    }

    function initForm() {
        $member = array(
            'oldPassword' => '',
            'newPassword' => '',
            'confNewPassword' => '',
        );
        $this->data['member'] = $member;
    }

    function gotoNextPage() {
        $forgetUsername = $this->input->post('forgetUsername');
        $forgetCitizenId = $this->input->post('forgetCitizenId');
        $this->load->model("Member_Model");
        $result = $this->Member_Model->getMemberByUserAndCitizen($forgetUsername, $forgetCitizenId);
        if (sizeof($result->row()) > 0) {
            $this->data['obj_member'] = $result;
            $this->data['main_content'] = 'change_forget_pwd';
            $this->load->view('includes/template', $this->data);
        } else {
            $this->data['alert_msg'] = 'alert("Username or CitizenId is incorrect.");';
            $this->data['main_content'] = 'forget_password';
            $this->load->view('includes/template', $this->data);
        }
    }

    function SubmitChangePwd() {
        $forgetIdMem = $this->input->post('idMember');
        $forgetNewPassword = $this->input->post('newPassword');
        $member = array(
            'PASSWORD' => MD5($forgetNewPassword)
        );
        $this->load->model("Member_Model");
        $this->Member_Model->updateMember($member, $forgetIdMem);
        $this->data['alert_msg'] = 'alert("The password change is complete");';
        $this->data['main_content'] = 'forget_password';
        $this->load->view('includes/template', $this->data);
    }

    function checkUsername() {
        $this->username = $_POST["username"];
        $this->load->model("Member_Model");
        $objMember = $this->Member_Model->getMemberByUsername($this->username);
        $obj = $objMember->row();
        if (!is_null($obj->USERNAME)){
            echo "true";
        } else {
            echo "false";
        }
    }
    
    function checkCitizenIdByUsername() {

        $this->username = $_POST["username"];
        $this->citizenId = $_POST["citizenId"];
        $this->load->model("Member_Model");
        $objMember = $this->Member_Model->getMemberByUsername($this->username);
        $obj = $objMember->row();
        if (!is_null($obj->ID_CARD)) {
            if($obj->ID_CARD == $this->citizenId){
                echo "true";
            }else{
                 echo "false";
            }
        }else{
             echo "false";
        }
       
    }

}

?>
